package com.lvtulife.common.component.message;

import com.lvtulife.common.util.db.CommonSql;
import com.lvtulife.common.util.db.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class BaseMessage {
    private static Logger logger = LoggerFactory.getLogger(BaseMessage.class);

    private static BaseMessage bean = null;
    private List<BaseMessageCode> list;

    private HashMap<Integer, Byte> codeTypes;
    private HashMap<Integer, Integer> codeValues;
    private HashMap<Integer, String> codeInfos;
    private HashMap<Integer, BaseMessageCode> codeBeans;

    public BaseMessage() {
        // 初始化
        list = new ArrayList<BaseMessageCode>();
        codeTypes = new HashMap<Integer, Byte>();
        codeValues = new HashMap<Integer, Integer>();
        codeInfos = new HashMap<Integer, String>();
        codeBeans = new HashMap<Integer, BaseMessageCode>();
    }

    public static BaseMessage getInstance() {
        if (bean == null) {
            //logger.info("编号代码加载中...");
            bean = new BaseMessage();
            /*String[] ctxFileName = new String[] { "spring.xml"};
            ApplicationContext ctx = new ClassPathXmlApplicationContext(ctxFileName);
			BaseMessageCodeServiceI service = (BaseMessageCodeServiceI) ctx.getBean("BaseMessageCodeService");
			service.initBaseMessageCode();*/

            /*BaseMessageCodeServiceI service = (BaseMessageCodeServiceI) SpringContextUtil.getBean("BaseMessageCodeService");
            List<BaseMessageCode> allCodes = service.findBaseMessageCodes();*/


            Configuration rc = new Configuration("db.properties");// 相对路径
            CommonSql commonSql = new CommonSql(rc.getValue("jdbc.driver"), rc.getValue("jdbc.url"), rc.getValue("jdbc.username"), rc.getValue("jdbc.password"));
            List<BaseMessageCode> allMsg = new ArrayList<>(0);
            try {
                if (commonSql.openConnection()) {
                    logger.info("数据库连接成功！");

                    DatabaseMetaData dbMetaData = commonSql.getConnection().getMetaData();
                    logger.info("当前连接的数据库是:" + dbMetaData.getDatabaseProductName());
                    logger.info(" " + dbMetaData.getDatabaseProductVersion());

                    ResultSet rs = commonSql.execQuery("select * from base_message_code where del =0 order by code_type,code_value");
                    while (rs.next()) {

                        BaseMessageCode msg = new BaseMessageCode();
                        msg.setId(rs.getInt("id"));
                        msg.setCodeType(rs.getByte("code_type"));
                        msg.setCodeValue(rs.getInt("code_value"));
                        msg.setCodeInfo(rs.getString("code_info"));
                        msg.setDel(rs.getByte("del"));

                        allMsg.add(msg);
                    }
                } else {
                    logger.info("数据库连接失败！");
                }
            } catch (Exception e) {
                logger.info("数据库连接失败！");
                e.printStackTrace();
            } finally {
                commonSql.closeAll();
            }

            bean.setList(allMsg);
            Iterator<BaseMessageCode> iter = allMsg.iterator();
            while (iter.hasNext()) {
                BaseMessageCode codeTemp = iter.next();
                bean.getCodeTypes().put(codeTemp.getCodeValue(), codeTemp.getCodeType());
                bean.getCodeValues().put(codeTemp.getCodeValue(), codeTemp.getCodeValue());
                bean.getCodeInfos().put(codeTemp.getCodeValue(), codeTemp.getCodeInfo());
                bean.getCodeBeans().put(codeTemp.getCodeValue(), codeTemp);
            }
            logger.info("编号代码加载完成");
        }
        return bean;
    }

    public static BaseMessage getBean() {
        return bean;
    }

    public static void setBean(BaseMessage bean) {
        BaseMessage.bean = bean;
    }

    public List<BaseMessageCode> getList() {
        return list;
    }

    public void setList(List<BaseMessageCode> list) {
        this.list = list;
    }

    public HashMap<Integer, Byte> getCodeTypes() {
        return codeTypes;
    }

    public void setCodeTypes(HashMap<Integer, Byte> codeTypes) {
        this.codeTypes = codeTypes;
    }

    public HashMap<Integer, Integer> getCodeValues() {
        return codeValues;
    }

    public void setCodeValues(HashMap<Integer, Integer> codeValues) {
        this.codeValues = codeValues;
    }

    public HashMap<Integer, String> getCodeInfos() {
        return codeInfos;
    }

    public void setCodeInfos(HashMap<Integer, String> codeInfos) {
        this.codeInfos = codeInfos;
    }

    public HashMap<Integer, BaseMessageCode> getCodeBeans() {
        return codeBeans;
    }

    public void setCodeBeans(HashMap<Integer, BaseMessageCode> codeBeans) {
        this.codeBeans = codeBeans;
    }

}
